package com.ttc.group2.videochat.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.quickblox.auth.QBAuth;
import com.quickblox.auth.model.QBSession;
import com.quickblox.core.QBEntityCallbackImpl;
import com.quickblox.core.QBSettings;
import com.quickblox.users.QBUsers;
import com.quickblox.users.model.QBUser;
import com.ttc.group2.videochat.R;
import com.ttc.group2.videochat.model.DataHolder;
import com.ttc.group2.videochat.utils.Const;
import com.ttc.group2.videochat.utils.DialogUtils;

public class SplashActivity extends Activity {
	protected static final String TAG = "SplashActivity";
	private Context mContext;
	private ProgressBar loadingBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.activity_splash);

		initView();

		// Authentication QuickBlox application.
		QBSettings.getInstance().fastConfigInit(Const.APP_ID, Const.AUTH_KEY, 
				Const.AUTH_SECRET);

		// Create QuickBlox session
		QBAuth.createSession(new QBEntityCallbackImpl<QBSession>() {
			@Override
			public void onSuccess(QBSession qbSession, Bundle bundle) {
				getAllUser();
			}

			@Override
			public void onError(List<String> errors) {
				// print errors that came from server
				DialogUtils.showToast(mContext, errors.get(0),
						Toast.LENGTH_SHORT);
				loadingBar.setVisibility(View.INVISIBLE);
			}
		});
	}

	private void initView() {
		loadingBar = (ProgressBar) findViewById(R.id.splash_loading_bar);
		loadingBar.setVisibility(View.VISIBLE);
	}

	private void startMainActivity() {
		startActivity(new Intent(mContext, MainActivity.class));
		finish();
	}

	private void getAllUser() {
		QBUsers.getUsers(null, new QBEntityCallbackImpl<ArrayList<QBUser>>() {
			@Override
			public void onSuccess(ArrayList<QBUser> qbUsers, Bundle bundle) {
				DataHolder.getDataHolder().setQbUsersList(qbUsers);
				startMainActivity();
			}

			@Override
			public void onError(List<String> errors) {
				DialogUtils.showToast(mContext, errors.get(0),
						Toast.LENGTH_SHORT);
			}
		});
	}

	/** Loading to MainActivity if no connection */
	private class LoadingActivity extends AsyncTask<Void, Void, Void> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			loadingBar.setVisibility(View.VISIBLE);
		}

		@Override
		protected Void doInBackground(Void... params) {
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			loadingBar.setVisibility(View.INVISIBLE);
			startMainActivity();
		}
	}

}
